package com.yanggu.mapreduce.manyjob;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/8 20:37
 * @description:
 */
public class OneJobMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private String fileName;

    private Text k = new Text();

    private IntWritable v = new IntWritable(1);

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        //获取文件的名称
        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        fileName = inputSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        //1. 获取一行数据
        String line = value.toString();

        //2. 进行切割
        String[] fields = line.split(" ");

        for (String field : fields) {
            //3. 封装数据
            k.set(field + "--" + this.fileName);

            //4. 写出
            context.write(k, v);
        }

    }
}
